// import VueRouter from 'vue-router'//引入路由
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import Login from '../components/Login.vue'
import Page404 from '../components/Page404.vue'
import Home from '../components/Home.vue'
import Layout from '../components/Layout.vue'
import Welcome from '../components/Welcome.vue'
import About from '../components/About.vue'
import Learn from '../components/Learn.vue'
import Password from '../components/Password.vue'
import Dict from '../components/Dict.vue'
import Plug from '../components/Plug.vue'
import {getCookie} from '../utils/cookies'
const routes = [
  { path: '/login', component: Login },
  {path:'/learn',component:Learn},
  {
    path: '/home',
    component: Layout,
    children:[
      {
        path:'welcome',
        component:Welcome,
      },
      {
        path:'about',
        component:About,
      },
      {
        path:'plug',
        component:Plug, 
      },
      {
        path:'password',
        component:Password, 
      },
      {
        path:'dict',
        component:Dict, 
      }
    ]
  },
  // components: ()=>import('../components/Home.vue')//懒加载
  { path: '/:catchAll(.*)', name: '404', component: Page404 },
]

// 3. 创建路由实例并传递 `routes` 配置
// 你可以在这里输入更多的配置，但我们在这里
// 暂时保持简单
const router = createRouter({
  // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
  history:  createWebHistory(),
  routes, // `routes: routes` 的缩写
})
//路由拦截(路由守卫)
router.beforeEach(
  (to, from) => {
    if (to.path === '/') {
      //获取token
      let token=getCookie('token')
      console.log(token)
      if(!token||token=='0'){
        return '/login'
      }
      else{
        return '/home/welcome'
      }
      
    }
    if(to.path === '/home'){
      return '/home/welcome'
    }
  }
)
export default router